home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / Unsupported Libraries / CustomIO_Lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-14  |  5.9 KB  |  252 lines  |  [TEXT/MPS ]

  1. /*
  2.  * CustomIO_Lib.c
  3.  *
  4.  *    Demo/Template for writing custom IO functions
  5.  *    
  6.  *    Custom Attribute example
  7.  */
  8. #include "QD3D.h"
  9.  
  10. #if defined(QD3D_OBSOLETE) && QD3D_OBSOLETE        
  11. #else  /*  QD3D_OBSOLETE  */
  12. #include "QD3DExtension.h"
  13. #endif  /*  QD3D_OBSOLETE  */
  14.  
  15. #include "QD3DSet.h"
  16. #include "QD3DIO.h"
  17. #include "CustomIO_Lib.h"
  18.  
  19. /*
  20.  * Globals
  21.  */
  22. #if defined(QD3D_OBSOLETE) && QD3D_OBSOLETE        
  23. static TQ3ObjectClass gCustomAttributeClass = NULL;
  24. #else  /*  QD3D_OBSOLETE  */
  25. static TQ3XObjectClass gCustomAttributeClass = NULL;
  26. #endif  /*  QD3D_OBSOLETE  */
  27.  
  28. static TQ3ElementType  gCustomAttributeType  = kQ3ElementTypeNone;
  29.  
  30. /*
  31.  * Static Functions
  32.  */
  33.  
  34. static TQ3Status CustomAttribute_Traverse(
  35.     TQ3Object                unused,
  36.     CustomAttributeRecord    *customAttribute,
  37.     TQ3ViewObject            view)
  38. {
  39.     (void) unused;
  40.     
  41.     if (customAttribute->clickModel == NULL)
  42.         return kQ3Success;
  43.  
  44. #if defined(QD3D_OBSOLETE) && QD3D_OBSOLETE        
  45.     if (Q3View_SubmitWriteData(
  46.         view, sizeof(TQ3Uns32), customAttribute, NULL) == kQ3Failure)
  47. #else  /*  QD3D_OBSOLETE  */
  48.     if (Q3XView_SubmitWriteData(
  49.         view, sizeof(TQ3Uns32), customAttribute, NULL) == kQ3Failure)
  50. #endif  /*  QD3D_OBSOLETE  */
  51.     {
  52.         return kQ3Failure;
  53.     }
  54.  
  55.     if (Q3Object_Submit(customAttribute->clickModel, view) == kQ3Failure)
  56.         return kQ3Failure;
  57.  
  58.     return kQ3Success;
  59. }
  60.  
  61. static TQ3Status CustomAttribute_Write(
  62.     CustomAttributeRecord    *customAttribute,
  63.     TQ3FileObject            file)
  64. {
  65.     return 
  66.         Q3Uns32_Write((TQ3Uns32) customAttribute->isOn, file) == kQ3Success &&
  67.         Q3Comment_Write("isOn", file) == kQ3Success ? kQ3Success : kQ3Failure;
  68. }
  69.  
  70. static TQ3Status CustomAttribute_ReadData(
  71.     TQ3AttributeSet            attributeSet,
  72.     TQ3FileObject            file)
  73. {
  74.     CustomAttributeRecord    customAttribute;
  75.     TQ3Status                status;
  76.     
  77.     if (Q3Uns32_Read((TQ3Uns32 *) &customAttribute.isOn, file) == kQ3Failure)
  78.         return kQ3Failure;
  79.  
  80.     customAttribute.clickModel = NULL;
  81.  
  82.     while (Q3File_IsEndOfContainer(file, NULL) == kQ3False)
  83.     {
  84.         customAttribute.clickModel = Q3File_ReadObject(file);
  85.  
  86.         if (customAttribute.clickModel == NULL)
  87.             return kQ3Failure;
  88.  
  89.         if (Q3Object_IsType(customAttribute.clickModel, kQ3GroupTypeDisplay))
  90.             break;
  91.         
  92.         Q3Object_Dispose(customAttribute.clickModel);
  93.         customAttribute.clickModel = NULL;
  94.     }
  95.         
  96.     status = Q3Set_Add(attributeSet, kCustomAttributeType, &customAttribute);
  97.  
  98.     if (customAttribute.clickModel)
  99.         Q3Object_Dispose(customAttribute.clickModel);
  100.  
  101.     return status;
  102. }
  103.  
  104. static TQ3Status CustomAttribute_CopyAdd(
  105.     CustomAttributeRecord    *src,
  106.     CustomAttributeRecord    *dst)
  107. {
  108.     *dst = *src;
  109.     if (dst->clickModel != NULL) 
  110.         Q3Shared_GetReference(dst->clickModel);
  111.     
  112.     return kQ3Success;
  113. }
  114.  
  115. static TQ3Status CustomAttribute_CopyReplace(
  116.     CustomAttributeRecord    *src,
  117.     CustomAttributeRecord    *dst)
  118. {
  119.     if (src->clickModel != NULL)
  120.         Q3Shared_GetReference(src->clickModel);
  121.     if (dst->clickModel != NULL) 
  122.         Q3Object_Dispose(dst->clickModel);
  123.  
  124.     *dst = *src;
  125.  
  126.     return kQ3Success;
  127. }
  128.  
  129. static TQ3Status CustomAttribute_Delete(
  130.     CustomAttributeRecord    *src)
  131. {
  132.     if (src->clickModel != NULL) 
  133.         Q3Object_Dispose(src->clickModel);
  134.     return kQ3Success;
  135. }
  136.  
  137. TQ3Status CustomAttribute_Unregister(
  138.     void)
  139. {
  140.     TQ3Status    status;
  141.  
  142.     status = kQ3Failure;
  143.  
  144.     if ( gCustomAttributeClass != NULL ) {
  145. #if defined(QD3D_OBSOLETE) && QD3D_OBSOLETE        
  146.         status = Q3ObjectClass_Unregister(gCustomAttributeClass);
  147. #else  /*  QD3D_OBSOLETE  */
  148.         status = Q3XObjectHierarchy_UnregisterClass(gCustomAttributeClass);
  149. #endif  /*  QD3D_OBSOLETE  */
  150.         gCustomAttributeClass = NULL;
  151.  
  152.         gCustomAttributeType = kQ3ElementTypeNone;
  153.     }
  154.  
  155.     return status;
  156. }
  157.  
  158. /*
  159.  * CustomAttribute_MetaHandler
  160.  */
  161. #if defined(QD3D_OBSOLETE) && QD3D_OBSOLETE        
  162. static TQ3FunctionPointer CustomAttribute_MetaHandler(
  163.     TQ3MethodType        methodType)
  164. {
  165.     switch (methodType)
  166.     {
  167.         case kQ3MethodTypeObjectTraverse:
  168.             return (TQ3FunctionPointer) CustomAttribute_Traverse;
  169.         case kQ3MethodTypeObjectWrite:
  170.             return (TQ3FunctionPointer) CustomAttribute_Write;
  171.         case kQ3MethodTypeObjectReadData:
  172.             return (TQ3FunctionPointer) CustomAttribute_ReadData;
  173.         case kQ3MethodTypeElementCopyAdd:
  174.         case kQ3MethodTypeElementCopyGet:
  175.         case kQ3MethodTypeElementCopyDuplicate:
  176.             return (TQ3FunctionPointer) CustomAttribute_CopyAdd;
  177.         case kQ3MethodTypeElementCopyReplace:
  178.             return (TQ3FunctionPointer) CustomAttribute_CopyReplace;
  179.         case kQ3MethodTypeElementDelete:
  180.             return (TQ3FunctionPointer) CustomAttribute_Delete;
  181.         default:
  182.             return (TQ3FunctionPointer) NULL;
  183.     }
  184. }
  185. #else  /*  QD3D_OBSOLETE  */
  186. static TQ3XFunctionPointer CustomAttribute_MetaHandler(
  187.     TQ3XMethodType        methodType)
  188. {
  189.     switch (methodType)
  190.     {
  191.         case kQ3XMethodTypeObjectTraverse:
  192.             return (TQ3XFunctionPointer) CustomAttribute_Traverse;
  193.         case kQ3XMethodTypeObjectWrite:
  194.             return (TQ3XFunctionPointer) CustomAttribute_Write;
  195.         case kQ3XMethodTypeObjectReadData:
  196.             return (TQ3XFunctionPointer) CustomAttribute_ReadData;
  197.         case kQ3XMethodTypeElementCopyAdd:
  198.         case kQ3XMethodTypeElementCopyGet:
  199.         case kQ3XMethodTypeElementCopyDuplicate:
  200.             return (TQ3XFunctionPointer) CustomAttribute_CopyAdd;
  201.         case kQ3XMethodTypeElementCopyReplace:
  202.             return (TQ3XFunctionPointer) CustomAttribute_CopyReplace;
  203.         case kQ3XMethodTypeElementDelete:
  204.             return (TQ3XFunctionPointer) CustomAttribute_Delete;
  205.         default:
  206.             return (TQ3XFunctionPointer) NULL;
  207.     }
  208. }
  209. #endif  /*  QD3D_OBSOLETE  */
  210.  
  211.  
  212. /*
  213.  * CustomAttribute_Register
  214.  */
  215. TQ3Status CustomAttribute_Register(
  216.     void)
  217. {
  218. #if defined(QD3D_OBSOLETE) && QD3D_OBSOLETE
  219.  
  220.     /* Old API compatibility */
  221.     gCustomAttributeClass = 
  222.         Q3ElementClass_Register(
  223.             kCustomAttributeType,
  224.             "CustomAttribute",
  225.             sizeof(CustomAttributeRecord),
  226.             CustomAttribute_MetaHandler);
  227.             
  228.     return (gCustomAttributeClass == NULL ? kQ3Failure : kQ3Success);
  229.  
  230. #else /* QD3D_OBSOLETE */
  231.  
  232.     gCustomAttributeClass = 
  233.         Q3XElementClass_Register(
  234.             &gCustomAttributeType,
  235.             "CustomAttribute",
  236.             sizeof(CustomAttributeRecord),
  237.             CustomAttribute_MetaHandler);
  238.  
  239.     return (gCustomAttributeClass == NULL ? kQ3Failure : kQ3Success);
  240. #endif /* QD3D_OBSOLETE */
  241. }
  242.  
  243.  
  244. /*
  245.  * CustomAttribute_GetType
  246.  */
  247. TQ3ElementType CustomAttribute_GetType(
  248.     void)
  249. {
  250.     return gCustomAttributeType;
  251. }
  252.